package cn.com.godliu.rabbitmqprovider.consumer;


import cn.com.godliu.rabbitmqprovider.config.JRedisConfig;
import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import io.swagger.annotations.Api;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.annotation.RabbitListeners;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
@Api(tags = "直连接收器")
public class DirectReceiver {

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    //@RabbitHandler
    //@RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueue
    //public void process(String hello,Channel channel, Message message) {
    //    System.out.println("DirectReceiver消费者收到消息  : " + message.toString());
    //    ackMethod(channel,message);
    //}
    //@RabbitHandler
    @RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueue
    public void process(Object testMessage) {
        System.out.println("DirectReceiver消费者收到消息  : " + testMessage.toString());

    }

    /**
     * 确认消息
     *
     * @param channel
     * @param message
     */
    @SneakyThrows
    private void ackMethod(Channel channel, Message message) {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }

    /**
     * 异常消息重试
     *
     * @param channel
     * @param message
     */
    @SneakyThrows
    private void nackMethod(Channel channel, Message message) {
        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,// false 继续消费，true消息丢失
                true);
        // 异常阻塞十秒
        Thread.sleep(10000);
    }
}
